﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Web;
using System.Net.Configuration;
using System.Threading;
using System.Net.Mail;
using LinkCard.Model;

namespace LinkCard.Utils
{
    public static class Utilities
    {
        public static List<string> GetProductImage(string productimg)
        {
            if (string.IsNullOrEmpty(productimg))
                return new List<string>();
            Regex rg = new Regex("<img.*? src=\"(?<imgurl>.*?)\".*?>", RegexOptions.IgnoreCase);
            Match rs = rg.Match(productimg);
            List<string> imgurls = new List<string>();
            string src = "";
            string cpdomain = ConfigurationManager.AppSettings["AdminDomain"];
            while (rs.Success)
            {
                src = rs.Groups["imgurl"].Value;
                if (!src.StartsWith("http"))
                {
                    src = cpdomain + src;
                }
                imgurls.Add(src);
                rs = rs.NextMatch();
            }
            return imgurls;
            //return (from item in imgurls select new string { url = item }).ToList();
        }

        public static string GetProductDetailUrl(object productid, object productname, object parentid = null)
        {
            long parentcatid = parentid != null ? (long)parentid : 0;
            string unsignname = ConvertTextToUrl(productname.ToString());
            return "/san-pham-" + productid.ToString() + "/m-2/p-" + parentcatid + "/" + unsignname + ".html";
        }

        public static string GetProductImage(object imagestring)
        {
            string s = "";
            if (imagestring != null)
            {
                s = Utils.Utilities.GetProductImage(imagestring.ToString()).FirstOrDefault();
                if (string.IsNullOrEmpty(s))
                {
                    s = "/template/Justin/images/no-image.jpg";
                }
            }
            else
            {
                s = "/template/Justin/images/no-image.jpg";
            }
            return s;
        }

        public static string ConvertTextToUrl(string text)
        {
            if (string.IsNullOrEmpty(text))
                return "";
            string Url = StripDiacritics(HttpUtility.HtmlDecode(text.ToLower()));

            Regex reg1 = new Regex(@"!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\""|\&|\#|\[|\]|~|$|_|“");
            Url = reg1.Replace(Url, "-");
            //fix ki tu |
            Url = Url.Replace("|", "-");

            reg1 = new Regex(@"-+-");
            Url = reg1.Replace(Url, "-");
            reg1 = new Regex(@"^\-+|\-+$");
            Url = reg1.Replace(Url, "");
            if (Url.Length > 190)
                Url = Url.Substring(0, 190);

            //xoa - o 2 dau
            reg1 = new Regex(@"^\-+|\-+$");
            Url = reg1.Replace(Url, "");
            return Url;
        }

        public static string StripDiacritics(string accented)
        {
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = accented.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

    }
}
